/*
 * @Author: szx
 * @Date: 2022-02-18 12:44:57
 * @LastEditTime: 2022-02-18 12:52:03
 * @Description:
 * @FilePath: \leetcode\200-299\219\219.js
 */
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var containsNearbyDuplicate = function (nums, k) {
    for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < Math.min(i + k + 1, nums.length); j++) {
            if (nums[i] == nums[j]) return true;
        }
    }
    return false;
};

var containsNearbyDuplicate = function (nums, k) {
    const set = new Set();
    for (let i = 0; i < nums.length; i++) {
        if (i > k) {
            set.delete(nums[i - k - 1]);
        }
        if (set.has(nums[i])) {
            return true;
        }
        set.add(nums[i]);
    }
    return false;
};
console.log(containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2));
